//
//  EncryptTool.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2017/8/31.
//  Copyright © 2017年 niuwaketang. All rights reserved.
//

#import "EncryptTool.h"
#import "NYSecurityTools.h"
#import "RMUUid.h"
#import "NYSignManager.h"

@implementation EncryptTool
+ (instancetype)sharedInstance {
        static dispatch_once_t onceToken;
        static EncryptTool *instance = nil;
        dispatch_once(&onceToken,^{
                instance = [[super alloc] init];
        });
        return instance;
}

- (NSDictionary *)encryptParam:(NSDictionary *)param
{
        if (!param) {
                return @{};
        }
        NSMutableDictionary *encryptParam = [NSMutableDictionary dictionaryWithDictionary:param];
        [encryptParam setObject:@((NSUInteger)(ceil([NSDate date].timeIntervalSince1970))).stringValue forKey:kRequestKeyTime];
        
        NSString *sortValueBuyKeys = [encryptParam.allValuesSortedByKeys componentsJoinedByString:@""].md5String;
        NSString *token = [NSString stringWithFormat:@"api_%@_api",sortValueBuyKeys].md5String;
        
        [encryptParam jk_setObj:token forKey:kRequestKeyToken];
        return encryptParam;
}

- (id)decryptObjectFromResponseString:(NSString *)responseString
{
        return responseString;
}
- (NSData *)decryptDataFromResponseString:(NSString *)responseString
{
        return [responseString dataUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)decryptStringFromResponseString:(NSString *)responseString
{
        return responseString;
}
- (NSString *)decryptStringFromResponseData:(NSData *)responseData
{
        if (!responseData) {
                return nil;
        }
        return [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];;
}
- (NSData *)decryptDataFromResponseData:(NSData *)responseData
{
        return responseData;
}
- (id)decryptObjectFromResponseData:(NSData *)responseData
{
        if (!responseData) {
                return nil;
        }
        NSString *decryptString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        return decryptString.jsonValueDecoded;
}

@end
